﻿using System;
using System.Collections.Generic;
using viForum.Classes;
using viForum.Controllers;
using visinia.SmartEngine;
using visinia.SmartEngine.Modules;

public partial class Modules_viForum_ThreadList_ThreadList : WebModule
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //set forum theme
        string themeCSS = WebRoots.GetModulesRoot + "viForum/Themes/" + BaseController.ForumTheme + "/viForum.css";
        WebPageController.AddCssLink(Page, themeCSS, true);

        Bind();
    }

    private void Bind()
    {
        string forumidstr = Request.QueryString["ForumId"];
        if (!string.IsNullOrEmpty(forumidstr))
        {
            int forumid = 0;
            if (Int32.TryParse(forumidstr, out forumid))
            {
                Forum forum = ForumController.GetForum(forumid);
                if (forum != null)
                {
                    List<ThreadData> threaddataList = new List<ThreadData>();
                    List<Thread> threads = ThreadController.GetThreads(forum.ForumId);
                    foreach (Thread thread in threads)
                    {
                        ThreadData data = new ThreadData();
                        data.ThreadId = thread.ThreadId;
                        data.Title = thread.Title;
                        data.User = thread.UserName;
                        data.Url = thread.Url;
                        if (System.Threading.Thread.CurrentPrincipal.IsInRole("admin"))
                            data.IsAdmin = true;
                        else
                            data.IsAdmin = false;
                        int repliesCount = ReplyController.GetRepliesCount(thread.ThreadId);
                        if (repliesCount > 0)
                        {
                            data.PostCount = 1 + repliesCount; /* thread + replies*/
                            Reply latestReply = ReplyController.GetLatestReply(thread.ThreadId);
                            data.LatestPost = latestReply.Date.ToString() + " by " + latestReply.UserName;
                        }
                        else
                        {
                            data.PostCount = 1; /* thread */
                            data.LatestPost = thread.Date.ToString() + " - " + thread.UserName;
                        }
                        threaddataList.Add(data);
                        ThreadList.DataSource = threaddataList;
                        ThreadList.DataBind();
                    }
                }
            }
        }
    }

    public class ThreadData
    {
        private int _ThreadId;
        public int ThreadId { get { return _ThreadId; } set { _ThreadId= value; } }

        private string _User;
        public string User { get { return _User; } set { _User = value; } }

        private string _Url;
        public string Url { get { return _Url; } set { _Url = value; } }

        private string _Title;
        public string Title { get { return _Title; } set { _Title = value; } }

        private int _PostCount;
        public int PostCount { get { return _PostCount; } set { _PostCount = value; } }

        private string _LatestPost;
        public string LatestPost { get { return _LatestPost; } set { _LatestPost = value; } }

        private bool _IsAdmin;
        public bool IsAdmin { get { return _IsAdmin; } set { _IsAdmin = value; } }
    }

    protected void AddThread_Click(object sender, EventArgs e)
    {
        Response.Redirect(WebRoots.GetWebRoot + BaseController.viForumUrlSeparator + "/Editor/AddThread/" + Request.QueryString["ForumId"] + ".aspx");
    }

    protected void ThreadList_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "delete")
        {
            int threadid = 0;
            if (Int32.TryParse(e.CommandArgument.ToString(), out threadid))
            {
                Thread thread = ThreadController.GetThread(threadid);
                if (thread != null)
                {
                    ThreadController.DeleteThread(thread.ThreadId);
                    Bind();
                }
            }
        }
    }
}
